const todayPlanService = require('../services/todayPlanService');
let R = require('../result/r');

/**
 * todayPlanController
 * Controller 是业务入口，由 HTTP 路由解析后调用
 * 包含今日计划的增删改查功能
 */
class todayPlanController {
    /**
     * 列出所有今日计划
     */
    async listAll(ctx) {
        const list = await todayPlanService.listAll();
        ctx.body = R.ok().data(list);
    }

    /**
     * 查询分页
     */
    async queryPage(ctx) {
        const params = ctx.request.body;
        const list = await todayPlanService.queryPage(params);

        ctx.body = R.ok().data(list);
    }

    /**
     * 查询一条数据根据id
     */
    async getOneById(ctx) {
        const one = await todayPlanService.getOneById(ctx.params.id);
        ctx.body = R.ok().data(one);
    }

    /**
     * 创建一条今日计划
     */
    async add(ctx) {
        const todayPlan = ctx.request.body;
        const result = await todayPlanService.create(todayPlan);
        ctx.body = R.ok("新增成功").data(result);
    }

    /**
     * 删除一条今日计划
     * @param ctx Koa 的上下文参数
     */
    async deleteById(ctx) {
        //console.log(ctx.params.id)
        await todayPlanService.delete(ctx.params.id);
        ctx.body = R.ok("删除成功");
    }


    /**
     * 更新今日计划
     */
    async updateOneById(ctx) {
        const todayPlan = ctx.request.body;
        await todayPlanService.update(todayPlan);
        ctx.body = R.ok("更新成功");
    }


}

// 导出 Controller 的实例
module.exports = new todayPlanController();
